import type { AnyObject } from '@/types/category'
import { defineStore } from 'pinia'
import {ref} from 'vue'
import loginApi from '@/apis/login'
const useUserStore = defineStore("user", ()=>{
  let token=ref<string>()
  if(localStorage.getItem('xmtoken')){
    token.value=localStorage.getItem('xmtoken')!
  }
const userInfo=ref<any>()

//登录
const getToken=async (data:AnyObject)=>{
  try {
    let res=await loginApi.reqToken(data)
    if(res.code==200){
      token.value=res.data.token

      localStorage.setItem('xmtoken',res.data.token)
     

    }
  } catch (error) {
    console.log(error);
    
    
  }

}
const getUserInfo=async ()=>{
  try {
    let res=await loginApi.reqUserInfo()
    if(res.code==200){
      userInfo.value=res.data
    }
  } catch (error) {
    console.log(error);
    
  }
}
const clearUserInfo=()=>{
  localStorage.removeItem('xmtoken')
  userInfo.value=null
}
return{
  token,
  getToken,
  userInfo,
  getUserInfo,
  clearUserInfo

}

}


)

export default useUserStore